require 'bundler'

module Calabash
  module Android
    module Rake
      def self.find_server_repo_or_raise
        calabash_server_dir =
          File.expand_path(ENV['CALABASH_SERVER_PATH'] ||
                           File.join(File.dirname(__FILE__), '..', '..',
                                     'calabash-android-server'))

        unless File.exist?(calabash_server_dir) &&
            File.exists?(File.join(calabash_server_dir, "server", "calabash-js", "src"))
          raise %Q[
Expected to find the calabash-android-server repo at:

    #{File.expand_path(calabash_server_dir)}

Either clone the repo to that location with:

$ git clone --recursive git@github.com:calabash/calabash-android-server.git #{calabash_server_dir}

or set CALABASH_SERVER_PATH to point to your local copy of the server.

$ CALABASH_SERVER_PATH=/path/to/server bundle exec rake build

For full instuctions see: https://github.com/calabash/calabash-android/wiki/Building-calabash-android

]
        end
        calabash_server_dir
      end

      def self.test_server_version
        test_server_version_path = File.join(self.find_server_repo_or_raise, "version")
        File.read(test_server_version_path).chomp
      end

      def expect_versions_aligned
        if ENV["SKIP_VERSION_CHECK"] == "1"
          puts "Skipping gem/server version check"
        else
          test_server_dir = self.find_server_repo_or_raise

          if test_server_version != Calabash::Android::VERSION
            raise(%Q[
Expected server version:

    #{test_server_version}

to match gem version:

    #{Calabash::Android::VERSION}

])
          end
        end
      end

      def self.build_test_apk
        test_server_dir = self.find_server_repo_or_raise
        Dir.chdir(File.join(test_server_dir, "server")) do
          system("./gradlew clean assembleAndroidTest")
          STDOUT.sync = true

          if $?.exitstatus != 0
            puts "Could not build the test server. Please see the output above."
            exit $?.exitstatus
          else
            puts "Calabash Android Server was built successfully."
          end
        end

        FileUtils.rm_rf "test_servers"
        FileUtils.mkdir_p "test_servers"

        FileUtils.cp(File.join(test_server_dir,"TestServer.apk"),
                     File.join(File.dirname(__FILE__),
                               'lib/calabash-android/lib/TestServer.apk'))
        FileUtils.cp(File.join(test_server_dir, "AndroidManifest.xml"),
                     File.join(File.dirname(__FILE__),
                               'lib/calabash-android/lib/AndroidManifest.xml'))
      end
    end
  end
end

task :build_server do
  Calabash::Android::Rake.build_test_apk
end

Bundler::GemHelper.install_tasks

begin
  require "rspec/core/rake_task"
  RSpec::Core::RakeTask.new(:spec) do |task|
    task.pattern = "spec/lib/**{,/*/**}/*_spec.rb"
  end

  RSpec::Core::RakeTask.new(:unit) do |task|
    task.pattern = "spec/lib/**{,/*/**}/*_spec.rb"
  end

  RSpec::Core::RakeTask.new(:integration) do |task|
    task.pattern = "spec/integration/**{,/*/**}/*_spec.rb"
  end
rescue LoadError => _
end

begin
  require "yard"
  YARD::Rake::YardocTask.new do |t|
    # see .yardopts for options
  end
rescue LoadError => _
end

